<?php
/**
 * 
 *  clase base de los controladores del la aplicacion
 * 
 */

class BaseController{
    
    public $theme_name = "Default";
    
    public function __construct(){
		
    }
    
    public function redirec($url,$param=array()){
        
        if (is_array($param) and count($param) > 0){
            $parameter = "?";
            foreach ($param as $llave=>$value){
                $parameter .= $llave."=".$value."&";
            }
            $parameter = substr($parameter, 0,-1);
        }
        header("location:".$url.$parameter);
    }
    
    public function render($name_view){ // incluye el codigo en la plantilla o theme
    global $config;
        
        $theme = file_get_contents(PATH_ROOT.DIRECTORY_SEPARATOR."templete".DIRECTORY_SEPARATOR.$this->theme_name.".php");
        
        //colocando titulos ...
        
        if (stripos($theme,"{title}") !== FALSE){
            $theme = str_replace("{title}", $config['description_app']['title'], $theme);
        }
        
        //localizando el css y actualizando la ruta
        
        if (stripos($theme, "{path_css}") !== FALSE){
            $theme = str_replace("{path_css}", Utility::getPathCss(), $theme);        
        }
        
        // localizando el javascript y actualizando la ruta
        
        if (stripos($theme, "{path_javascript}") !== FALSE){
            $theme = str_replace("{path_javascript}", Utility::getPathJs(), $theme);        
        }
        
        if (stripos($theme,"{content}") !== FALSE){
            $theme_array = explode("{content}",$theme);
            print $theme_array[0];
            include PATH_ROOT.DIRECTORY_SEPARATOR.$name_view;
            print $theme_array[1];
        }
        else{
            die("NO SE ENCONTRO LA ETIQUETA DE CONTENIDO");
        }    
    }
    
    public function beginForm($url="#",$method="POST",$param=array()){
        
        $parameter = "";
        
        if (count($param) > 0){
            foreach ($param as $key=> $value){
                $parameter .= $key.'="'.$value.'" ';
            }
        }

        return '<form '.$parameter.' method="'.$method.'" action="'.$url.'">';
    }
    
    public function endForm(){
        return '</form>';
    }
    
    public function TEdit(){
        
        $tedit = new TEdit();
        return $tedit;
    }
    
    public function TLabel(){
        
        $tlabel = new TLabel();
        return $tlabel;
        
    }
    
    public function TButton(){
        $tbutton = new TButton();
        return $tbutton;
    }

}

?>